package com.example.backend.controller;

import com.example.backend.entity.Post;
import com.example.backend.model.BaseModel;
import com.example.backend.model.PostModel;
import com.example.backend.service.PostService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("post")
public class PostController {
    @Resource
    private PostService postService;

    @Resource
    HttpSession session;

    @Resource
    HttpServletRequest request;

    @RequestMapping(value = "getAllPosts", method = RequestMethod.GET)
    public PostModel getAllPosts(){
        List<Post> posts=postService.getAllPosts();
        if(posts!=null){
            return new PostModel(0,"success",posts);
        }
        else
            return new PostModel(1,"error",null);
    }

    @RequestMapping(value = "addPosts", method = RequestMethod.POST)
    public BaseModel addPosts(){
        int postId = Integer.parseInt(request.getParameter("postId"));
        int userId = Integer.parseInt(request.getParameter("userId"));
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String date = request.getParameter("date");
        if(postService.addPosts(postId,userId,title,content,date))
            return new BaseModel(0,"success");
        else
            return new BaseModel(1,"error");
    }

    @RequestMapping(value = "getPosts",method = RequestMethod.POST)
    public BaseModel getPosts(){
        String title = request.getParameter("title");
        List<Post> posts = postService.getPosts(title);
        if(posts!=null)
            return new PostModel(0,"success",posts);
        else
            return new PostModel(1,"error",null);
    }

    @RequestMapping(value = "deletePosts",method = RequestMethod.POST)
    public BaseModel deletePosts(){
        int postId = Integer.parseInt(request.getParameter("postId"));
        if (postService.deletePosts(postId))
            return new BaseModel(0,"success");
        else
            return new BaseModel(1,"error");
    }

    @RequestMapping(value = "updatePosts",method = RequestMethod.POST)
    public BaseModel updatePosts(){
        String postId = request.getParameter("postId");
        String userId = request.getParameter("userId");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String date = request.getParameter("date");
        Post post = new Post();
        post.setPostId(Integer.parseInt(postId));
        post.setUserId(Integer.parseInt(userId));
        post.setTitle(title);
        post.setContent(content);
        post.setDate(date);
        if(postService.updatePosts(post.getPostId(), post.getUserId(), post.getTitle(),post.getContent(),post.getDate()))
            return new BaseModel(0,"success");
        else
            return new BaseModel(1,"error");
    }
}
